package org.hydrakyoufeng.stuplatform.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.hydrakyoufeng.stuplatform.dto.*;
import org.hydrakyoufeng.stuplatform.mapper.ClassMapper;
import org.hydrakyoufeng.stuplatform.service.ClassService;
import org.hydrakyoufeng.stuplatform.utils.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class ClassServiceImpl implements ClassService {

    @Autowired
    private ClassMapper classMapper;

    @Autowired
    @Qualifier("adminIdTheadLocal")
    private ThreadLocal<Integer> adminIdTheadLocal;

    @Autowired
    @Qualifier("userIdTheadLocal")
    private ThreadLocal<Integer> userIdTheadLocal;

    @Override
    public Object queryList(ClassListDTO classListDTO) throws Exception {
        log.info("classListDTO = " + classListDTO);
        // 开启分页
        PageHelper.startPage(classListDTO.getCurrentPage(),
                classListDTO.getPageSize(),
                "update_time desc");
        PageInfo pageInfo = new PageInfo(classMapper.queryList(classListDTO));
        return ResultEntity.success("查询成功",pageInfo);
    }

    @Override
    public Object insert(ClassDTO classDTO) throws Exception {
        classDTO.setCreator_by(adminIdTheadLocal.get());
        classDTO.setUpdate_by(adminIdTheadLocal.get());
        int result = classMapper.insert(classDTO);
        if (result > 0) {
            return ResultEntity.success("添加成功");
        } else {
            return ResultEntity.error("添加失败");
        }
    }

    @Override
    public Object update(ClassDTO classDTO) throws Exception {
        classDTO.setUpdate_by(adminIdTheadLocal.get());
        int result = classMapper.update(classDTO);
        if (result > 0) {
            return ResultEntity.success("修改成功");
        } else {
            return ResultEntity.error("修改失败");
        }
    }

    @Override
    public Object delete(Integer id) throws Exception {
        ClassDTO classDTO = new ClassDTO();
        classDTO.setId(id);
        classDTO.setUpdate_by(adminIdTheadLocal.get());
        int result = classMapper.delete(classDTO);
        if (result > 0) {
            return ResultEntity.success("删除成功");
        } else {
            return ResultEntity.error("删除失败");
        }
    }

    /**
     * 分配课程，先将班级中的所有课程删除，在循环添加
     * 此处使用了事务
     * @param classCourseDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object updateCourse(ClassCourseDTO classCourseDTO) {
        classCourseDTO.setUpdate_by(adminIdTheadLocal.get());
        classCourseDTO.setCreator_by(adminIdTheadLocal.get());
        // 先将所有课程都删除
        classMapper.deleteCourse(classCourseDTO);
        List<Integer> list = classCourseDTO.getCourse_ids();
        // 因为要使用索引，所以需要遍历
        for (int i = 0; i < list.size(); i++) {
            classCourseDTO.setCourse_id(list.get(i));
            classCourseDTO.setSort(i);
            classMapper.insertCourse(classCourseDTO);

        }
        return ResultEntity.success("课程分配成功");
    }

    @Override
    public Object queryEntryList(ClassEntryListDTO classEntryListDTO) {
        classEntryListDTO.setUser_id(userIdTheadLocal.get());
        // 开启分页
        PageHelper.startPage(classEntryListDTO.getCurrentPage(),
                classEntryListDTO.getPageSize(),
                "cu.update_time DESC,c.update_time DESC");
        PageInfo pageInfo = new PageInfo(classMapper.queryEntryList(classEntryListDTO));
        return ResultEntity.success("查询成功",pageInfo);
    }

    @Override
    public Object entry(ClassEntryDTO classUserDTO) {
        classUserDTO.setUser_id(userIdTheadLocal.get());
        log.info("classUserDTO = " + classUserDTO);
        int result = classMapper.entry(classUserDTO);
        if (result > 0) {
            return ResultEntity.success("操作成功");
        } else {
            return ResultEntity.error("操作失败");
        }
    }

    @Override
    public Object queryCheckList(ClassCheckListDTO classCheckListDTO) {
        PageHelper.startPage(classCheckListDTO.getCurrentPage(),
                classCheckListDTO.getPageSize(),
                "cu.update_time DESC");
        PageInfo pageInfo = new PageInfo(classMapper.queryCheckList(classCheckListDTO));
        return ResultEntity.success("查询成功",pageInfo);
    }

    @Override
    public Object check(ClassCheckDTO classJoinDTO) {
        classJoinDTO.setUpdate_by(adminIdTheadLocal.get());
        int result = classMapper.check(classJoinDTO);
        if (result > 0) {
            return ResultEntity.success("操作成功");
        } else {
            return ResultEntity.error("操作失败");
        }
    }

    @Override
    public Object queryUserList() {
        return ResultEntity.success("查询成功",classMapper.queryUserList(userIdTheadLocal.get()));
    }
}
